package service

import (
	"context"
	"video-srv/basic/config"
	__ "video-srv/basic/proto/rpc"
	"video-srv/handler/model"
)

func (s *VideoServer) GetEpisodeList(_ context.Context, in *__.GetEpisodeListReq) (*__.GetEpisodeListResp, error) {
	var epi model.Episode
	list, err := epi.GetEpisodeList(config.DB, int(in.VideoId))
	if err != nil {
		return nil, err
	}
	log := model.EpisodeLog{
		VideoId:   int(in.VideoId),
		EpisodeId: int(epi.ID),
		Duration:  945824,
	}
	err = log.CreateEpisodeLog(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.GetEpisodeListResp{
		List: list,
	}, nil
}

func (s *VideoServer) CreateEpisodeLog(_ context.Context, in *__.CreateEpisodeLogReq) (*__.CreateEpisodeLogResp, error) {
	log := model.EpisodeLog{
		UserId:    int(in.UserId),
		VideoId:   int(in.VideoId),
		EpisodeId: int(in.EpisodeId),
		Duration:  int(in.Duration),
		LookTime:  int(in.LookTime),
	}
	err := log.CreateEpisodeLog(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.CreateEpisodeLogResp{
		LogId: int64(log.ID),
	}, nil
}

func (s *VideoServer) GetEpisodeLog(_ context.Context, in *__.GetEpisodeLogReq) (*__.GetEpisodeLogResp, error) {
	var log model.EpisodeLog
	episodeLog, err := log.GetEpisodeLog(config.DB, int(in.LogId), int(in.Page), int(in.Size))
	if err != nil {
		return nil, err
	}
	return &__.GetEpisodeLogResp{
		List: episodeLog,
	}, nil
}
